package com.tepth.vehiclemaintenance.ui.view.dialog;

import android.app.Dialog;
import android.content.Context;

import cn.pedant.SweetAlert.SweetAlertDialog;

/**
 * Created by zhangyu on 2017/5/10.
 */

public class SweetAlertDialogDelegate implements DialogDelegate {
    private SweetAlertDialog pDialog;
    private Context mContext;

    public SweetAlertDialogDelegate(Context context) {
        mContext = context;
    }

    @Override
    public Dialog getDialog() {
        return pDialog;
    }

    @Override
    public void showProgressDialog(boolean canCancel, String msg) {
        pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.PROGRESS_TYPE)
                .setTitleText(msg);
        pDialog.setCanceledOnTouchOutside(canCancel);
        pDialog.show();
    }

    @Override
    public void changeText(String title, String msg) {
        if (pDialog != null) {
            pDialog.setTitleText(title);
            pDialog.setContentText(msg);
        }
    }

    @Override
    public void showNormalDialog(String option, String msg, final OnDialogListener confirmListener, final OnDialogListener cancelListener) {
        pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.NORMAL_TYPE)
                .setTitleText(option)
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        confirmListener.onClick();
                    }
                }).setCancelClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        cancelListener.onClick();
                    }
                })
                .setContentText(msg);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    public void showWarningDialog(String option, String msg, final OnDialogListener listener) {
        pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.NORMAL_TYPE)
                .setTitleText(option)
                .setConfirmText("确定")
                .setCancelText("取消")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        listener.onClick();
                        clearDialog();
                    }
                })
                .setContentText(msg);
        pDialog.show();
    }

    @Override
    public void showWarningDialog(String option, String msg, final OnDialogListener listener, final boolean cancel) {
        pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.NORMAL_TYPE)
                .setTitleText(option)
                .setConfirmText("确定")
                .setCancelText("取消")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        listener.onClick();
                        if(cancel){
                            clearDialog();
                        }
                    }
                })
                .setContentText(msg);
        pDialog.show();
    }

    @Override
    public void showSuccessDialog(String option, String msg, final OnDialogListener listener) {
        pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.SUCCESS_TYPE)
                .setTitleText(option)
                .setConfirmText("确定")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        listener.onClick();
                    }
                })
                .setContentText(msg);
        pDialog.setCancelable(false);
        pDialog.show();
    }

    @Override
    public void showErrorDialog(String option, String msg, final OnDialogListener listener) {
        pDialog = new SweetAlertDialog(mContext, SweetAlertDialog.ERROR_TYPE)
                .setTitleText(option)
                .setConfirmText("确定")
                .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                    @Override
                    public void onClick(SweetAlertDialog sweetAlertDialog) {
                        listener.onClick();
                    }
                })
                .setContentText(msg);
        pDialog.show();
    }

    @Override
    public void stopProgressWithSuccess(String option, String msg, final OnDialogListener listener) {
        if (pDialog != null) {
            pDialog.setTitleText(option)
                    .setContentText(msg)
                    .setConfirmText("确定")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            listener.onClick();
                        }
                    })
                    .changeAlertType(SweetAlertDialog.SUCCESS_TYPE);
        }
    }

    @Override
    public void stopProgressWithFailed(String option, String msg) {
        if (pDialog != null) {
            pDialog.setTitleText(option)
                    .setContentText(msg)
                    .setConfirmText("确定")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            sweetAlertDialog.dismissWithAnimation();
                        }
                    })
                    .changeAlertType(SweetAlertDialog.ERROR_TYPE);
        }
    }

    @Override
    public void stopProgressWithWarning(String option, String msg, final OnDialogListener listener) {
        if (pDialog != null) {
            pDialog.setTitleText(option)
                    .setContentText(msg)
                    .setConfirmText("确定")
                    .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                        @Override
                        public void onClick(SweetAlertDialog sweetAlertDialog) {
                            listener.onClick();
                            clearDialog();
                        }
                    })
                    .setCancelText("取消")
                    .showCancelButton(true)
                    .changeAlertType(SweetAlertDialog.WARNING_TYPE);
        }
    }

    @Override
    public void clearDialog() {
        if (pDialog != null && pDialog.isShowing()) {
            pDialog.dismiss();
        }
    }
    @Override
    public void changeToProgressType(String msg) {
        if (pDialog != null) {
            pDialog.setTitleText(msg)
                    .changeAlertType(SweetAlertDialog.PROGRESS_TYPE);
        }
    }
}
